/*
PARI/GP scripts for primes-related sequences 
=============================================================
File: PrimesRelatedSequences.txt    
Link: https://oeis.org/wiki/File:PrimesRelatedSequences.txt
=============================================================
*/


PrimeByMplusD(nmax,m,d) =
/* ----------------------------------------------------------
  Lists nmax primes such that m*prime+d is also prime.
  Note: Hangs when no prime (m*prime+d) can be found! 
---------------------------------------------------------- */
{
  my(p,idx=1,n=1,v=vector(nmax));
  while (idx<=nmax,
    p = prime(n);
    if (isprime(m*p+d), v[idx] = p; idx++);
    n++;
  );
  return(v);
}

CodedPrimesSequence(nmax) =
/* ----------------------------------------------------------
  Returns a vector of naturals such that
  - if a(n) is 2 or an odd value, it is a singlet prime,
  - else a(n) lies between two twin primes
  Used to generate data for OEIS A167706
---------------------------------------------------------- */
{
  my (v=vector(nmax),n=1,k=1,p,pn,skip);
  v[n]=prime(k);
  k++;p=prime(k);
  while(1,
    pn = prime(k+1);
    skip = 0;
    while (pn==(p+2),
      k++; skip=1;
      n++; v[n]=p+1;
      if (n==nmax, return(v));
      p=pn; pn=prime(k+1);
    );
    if (!skip, n++;v[n]=p;if(n==nmax,return(v)));
    p=pn; k++; pn=prime(k+1);
  );
}


/*
=============================================================
   Contributed to OEIS Wiki by Stanislav Sykora
=============================================================
*/
